package com.zhp.farmerclient.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhp.farmerclient.common.Result;
import com.zhp.farmerclient.entity.Event;
import com.zhp.farmerclient.mapper.EventMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EventService {
    @Autowired
    public EventMapper eventMapper;

    //事件申请
    public Result apply(Event event){
        eventMapper.insert(event);
        return Result.succ();
    }


    //撤销申请/删除
    public Result delete(int id) {
        eventMapper.deleteById(id);
        return Result.succ();
    }

    //批量删除
    public Result deletes(List<Integer> ids) {
        eventMapper.deleteBatchIds(ids);
        return Result.succ();
    }

    //事件审批
    public Result accept(Event event){
        UpdateWrapper<Event> eventUpdateWrapper = new UpdateWrapper<>();
        eventUpdateWrapper.eq("id",event.getId());
        eventMapper.update(event,eventUpdateWrapper);
        return Result.succ();
    }

    //根据用户ID查看用户事件申请
    public Result check(Integer page, Integer size, Integer id) {
        Page<Event> eventPage = new Page<>(page,size);
        QueryWrapper<Event> queryWrapper =new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        queryWrapper.eq("user_id",id);
        IPage<Event> iPage = eventMapper.selectPage(eventPage,queryWrapper);
        return Result.succ(iPage.getRecords(),iPage.getTotal());
    }
    // 获取反馈详情
    public Result detail(int id){
        Event event = eventMapper.selectById(id);
        return Result.succ(event,null);
    }

    //分页显示（查询）
    public Result page(Integer pageNum, Integer pageSize, String title, String content, String status) {
        Page<Event> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        if (!"".equals(title)) {
            queryWrapper.like("title", title);
        }
        if (!"".equals(content)) {
            queryWrapper.like("content", content);
        }
        if (!"".equals(status)) {
            queryWrapper.like("status", status);
        }
        queryWrapper.orderByDesc("id");
        IPage<Event> iPage = eventMapper.selectPage(page,queryWrapper);
        return Result.succ(iPage.getRecords(),iPage.getTotal());
    }
}
